const {
  queryEnumTypeService,
  queryDictService,
  queryDictByIdService,
  queryNavTypeService,
  queryNavListService,
  queryFriendLinkService,
  modifyFriendLinkByIdService,
  modifyNavByIdService,
  addFriendLinkService,
  addNavControlService,
  queryLimitService,
  addLimitService,
  modifyLimitService,
  deleteLimitService
} = require("../service/common.service");

const fetch = require("superagent"); //请求工具

class commonController {
  async queryEnumType(ctx, next) {
    console.log(ctx.dictId, "ctx");
    const res = await queryEnumTypeService(ctx.dictId);
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: res,
    };
  }

  async queryDictControl(ctx, next) {
    const requestObj = ctx.request.body;
    const res = await queryDictService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }
  // 按id查询对应的枚举数据
  async queryDictByIdControl(ctx, next) {
    let { id } = ctx.request.body;
    const res = await queryDictByIdService(id);
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }

  // 导航类型
  async queryNavTypeControl(ctx, next) {
    const requestObj = ctx.request.body || {};
    const res = await queryNavTypeService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }
  //导航列表
  async queryNavListControl(ctx, next) {
    const requestObj = ctx.request.body || {};
    const res = await queryNavListService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }

  async modifyNavByIdControl(ctx, next) {
    const requestObj = ctx.request.body;
    const res = await modifyNavByIdService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: null,
    };
  }

  // 友链
  async queryFriendLinkControl(ctx, next) {
    const res = await queryFriendLinkService();
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }
  // 修改友联信息
  async modifyFriendLinkByIdControl(ctx, next) {
    const requestObj = ctx.request.body;
    const res = await modifyFriendLinkByIdService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: null,
    };
  }

  // 添加友联信息
  async addFriendLinkControl(ctx, next) {
    const requestObj = ctx.request.body;
    const res = await addFriendLinkService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: null,
    };
  }

  // 添加导航信息
  async addNavControl(ctx, next) {
    const requestObj = ctx.request.body;
    const res = await addNavControlService(requestObj);
    ctx.body = {
      code: 200,
      message: "success",
      data: null,
    };
  }

  // 热搜
  async queryHotControl(ctx, next) {
    let type = ctx.query.type;
    console.log(ctx.query, "带娃");
    let res;
    if (type === "csdn") {
      res = await fetch.get(
        "https://blog.csdn.net/phoenix/web/blog/hot-rank?page=0&pageSize=25"
      );
    } else if (type === "zhihu") {
      res = await fetch.get(
        "https://www.zhihu.com/api/v4/search/top_search/tabs/hot/items"
      );
    } else if (type === "weibo") {
      res = await fetch.get("https://weibo.com/ajax/side/hotSearch");
    } else if (type === "toutiao") {
      res = await fetch.get(
        "https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc&_signature=_02B4Z6wo00901u3clewAAIDCbd5vrxLVL3Lt-JFAAN7pok0tG52Vvynuiz04lx6vVkv2DQoPfl94zodIffAN21eo37F4BY1NPkGEDDyu-od5DeqUgf8hi.wNbIciXd5NU1dcEjyemmGLtCznad"
      );
    }
    let text = JSON.parse(res.text);
    console.log(res, "res");
    ctx.body = {
      data: text.data,
    };
  }
  // 权限管理查询
  async queryLimitControl(ctx, next) {
    const res = await queryLimitService();
    ctx.body = {
      code: 200,
      message: "success",
      data: res,
    };
  }

  async addLimitControl(ctx, next) {
    let requestObj = ctx.request.body;
    const res = await addLimitService(requestObj);
    ctx.body = {
      message: "success",
      code: 200,
      data: null,
    };
  }

  async modifyControl(ctx, next) {
    let requestObj = ctx.request.body;
    const res = await modifyLimitService(requestObj);
    ctx.body = {
      message: "success",
      code: 200,
      data: null,
    };
  }

  async deleteLimitControl(ctx, next) {
    let requestObj = ctx.request.body;
    const res = await deleteLimitService(requestObj.id);
    ctx.body = {
      message: "success",
      code: 200,
      data: null,
    };
  }
}

module.exports = new commonController();
